package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.Exception.BusinessException;
import com.itheima.factory.ResultFactory;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {
    //注入usermapper
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询
     *
     * @return
     */
    @Transactional(readOnly = true)
    public PageInfo<User> getAll(int page, int size) {
        PageHelper.startPage(page,size);
       List<User> userList = userMapper.getAll();
        return new PageInfo<User>(userList);
    }

    /**
     * 分页条件查询
     * @param user
     * @param page
     * @param size
     * @return
     */
    @Transactional(readOnly = true)
    public PageInfo<User> getUsers(User user ,int page, int size) {
        PageHelper.startPage(page,size);
        List<User> userList = userMapper.getUsers(user);
        return new PageInfo<User>(userList);
    }


    /**
     * 根据id查询
     *
     * @param uuid
     * @return
     */
    @Transactional(readOnly = true)
    public User get(int uuid) {
        return userMapper.get(uuid);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean save(User user) {
        return userMapper.save(user);
    }

    /**
     * 删除用户
     *
     * @param uuid
     * @return
     */
    @Override
    public boolean delete(int uuid) {
        return userMapper.delete(uuid);
    }

    /**
     * 根据id 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }

    /**
     * 根据用户名和密码查询
     *
     * @param username
     * @param password
     * @return
     */
    @Transactional(readOnly = true)
    public User login(String username, String password) {
        if (StringUtils.isEmpty(username)){
            throw new BusinessException(ResultFactory.Error("用户名不能为空"));
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException(ResultFactory.Error("密码不能为空"));
        }
        User user = userMapper.getUsernameAndPassword(username, password);

        if (user == null) throw  new BusinessException(ResultFactory.Error("用户名或者密码有误"));
        return user;
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    public boolean deleteByUuids(String ids) {
        if (ids!= null && ids.length()> 0){
            String[] arr = ids.split(",");
            return userMapper.deleteByUuids(arr);
        }
        return false;

    }
}
